<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简易手机通讯录管理</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        /* 全局样式 */
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background-color: #f0f2f5;
            margin: 0;
            padding: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
            color: #333;
        }

        /* 主容器样式 */
        .container {
            width: 90%;
            max-width: 600px;
            background-color: white;
            border-radius: 15px;
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
            padding: 30px;
            margin-top: 20px;
        }

        /* 标题样式 */
        h1 {
            color: #2c3e50;
            text-align: center;
            margin-top: 0;
            margin-bottom: 30px;
            font-size: 24px;
            font-weight: bold;
        }

        /* 表单组样式 */
        .form-group {
            margin-bottom: 20px;
            display: flex;
            align-items: center;
        }

        /* 标签样式 */
        .form-group label {
            width: 80px;
            font-weight: bold;
            color: #555;
        }

        /* 输入框样式 */
        .form-group input[type="text"] {
            flex: 1;
            padding: 10px 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            box-sizing: border-box;
            transition: border-color 0.3s;
        }

        .form-group input[type="text"]:focus {
            border-color: #4a90e2;
            outline: none;
            box-shadow: 0 0 8px rgba(74, 144, 226, 0.3);
        }

        /* 按钮容器样式 */
        .button-container {
            display: flex;
            justify-content: center;
            margin: 30px 0;
            gap: 15px;
        }

        /* 按钮样式 */
        button {
            padding: 10px 20px;
            border: none;
            border-radius: 25px;
            font-size: 15px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: bold;
            box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
        }

        #add {
            background-color: #4CAF50;
            color: white;
        }

        #search {
            background-color: #2196F3;
            color: white;
        }

        #delete {
            background-color: #f44336;
            color: white;
        }

        #reset {
            background-color: #9e9e9e;
            color: white;
        }

        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.15);
        }

        /* 通讯录列表样式 */
        .phone-list {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        .phone-list th, .phone-list td {
            border: 1px solid #eee;
            padding: 12px 15px;
            text-align: left;
        }

        .phone-list th {
            background-color: #f5f7fa;
            color: #2c3e50;
            font-weight: bold;
        }

        .phone-list tr:nth-child(even) {
            background-color: #fafafa;
        }

        .phone-list tr:hover {
            background-color: #f0f7ff;
        }

        /* 无数据时的样式 */
        .no-data {
            text-align: center;
            padding: 20px;
            color: #888;
        }

        /* 表单标题样式 */
        .form-title {
            color: #555;
            font-weight: bold;
            margin-bottom: 15px;
            font-size: 18px;
            border-bottom: 1px solid #eee;
            padding-bottom: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>简易手机通讯录管理</h1>

        <div class="form-group">
            <label for="name">姓名：</label>
            <input type="text" id="name" placeholder="请输入姓名">
        </div>

        <div class="form-group">
            <label for="phone">电话：</label>
            <input type="text" id="phone" placeholder="请输入电话号码">
        </div>

        <div class="button-container">
            <button id="add">添加</button>
            <button id="search">查询</button>
            <button id="delete">删除</button>
            <button id="reset">重置</button>
        </div>

        <div class="form-title">通讯录：</div>
        <table class="phone-list">
            <thead>
                <tr>
                    <th>姓名</th>
                    <th>电话</th>
                </tr>
            </thead>
            <tbody id="contact-list">
                <!-- 联系人列表将在这里动态生成 -->
            </tbody>
        </table>
    </div>

    <script>
        $(document).ready(function() {
            // 初始化通讯录数据
            let phonebook = JSON.parse(localStorage.getItem('phonebook')) || [];

            // 显示通讯录
            function displayPhonebook() {
                const contactList = $('#contact-list');
                contactList.empty();

                if (phonebook.length === 0) {
                    contactList.append('<tr><td colspan="2" class="no-data">暂无联系人</td></tr>');
                    return;
                }

                phonebook.forEach(contact => {
                    contactList.append(
                        `<tr>
                            <td>${contact.name}</td>
                            <td>${contact.phone}</td>
                        </tr>`
                    );
                });
            }

            // 添加联系人
            $('#add').click(function() {
                let name = $('#name').val().trim();
                let phone = $('#phone').val().trim();

                if (name && phone) {
                    // 检查是否存在相同姓名的联系人
                    const existingIndex = phonebook.findIndex(c => c.name === name);
                    if (existingIndex !== -1) {
                        alert('该姓名已存在');
                        return;
                    }

                    phonebook.push({name: name, phone: phone});
                    localStorage.setItem('phonebook', JSON.stringify(phonebook));
                    displayPhonebook();
                    alert('添加成功');
                    $('#reset').click(); // 重置表单
                } else {
                    alert('请输入姓名和电话');
                }
            });

            // 查询联系人
            $('#search').click(function() {
                let name = $('#name').val().trim();
                if (name) {
                    let contact = phonebook.find(c => c.name === name);
                    if (contact) {
                        $('#phone').val(contact.phone);
                    } else {
                        alert('未找到该联系人');
                        $('#phone').val('');
                    }
                } else {
                    alert('请输入姓名进行查询');
                }
            });

            // 删除联系人
            $('#delete').click(function() {
                let name = $('#name').val().trim();
                if (name) {
                    phonebook = phonebook.filter(c => c.name !== name);
                    localStorage.setItem('phonebook', JSON.stringify(phonebook));
                    displayPhonebook();
                    $('#name').val('');
                    $('#phone').val('');
                    alert('删除成功');
                } else {
                    alert('请输入要删除的联系人姓名');
                }
            });

            // 重置表单
            $('#reset').click(function() {
                $('#name').val('');
                $('#phone').val('');
            });

            // 初始显示通讯录
            displayPhonebook();
        });
    </script>
</body>
</html>